package com.example.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.reggie.domain.DishFlavor;
import com.example.reggie.domain.Setmeal;
import com.example.reggie.domain.SetmealDish;
import com.example.reggie.dto.SetmealDto;
import com.example.reggie.service.SetmealDishService;
import com.example.reggie.service.SetmealService;
import com.example.reggie.mapper.SetmealMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 27935
* @description 针对表【setmeal(套餐)】的数据库操作Service实现
* @createDate 2024-08-03 12:00:49
*/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService{

    @Autowired
    SetmealDishService setmealDishService;

    @Override
    public void setmealAdd(SetmealDto setmealDto) {
        this.save(setmealDto);
        Long id = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (int i=0;i<setmealDishes.size();i++){
            setmealDishes.get(i).setSetmealId(id);
            setmealDishService.save(setmealDishes.get(i));
        }
    }

    @Override
    public void setmealUpdate(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }
}




